// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// import AudioManage, {soundList} from "../resources/framework/AudioManage";
import {defaultType} from "./defaultTypeMediator";
import AudioManage, {soundList} from "../framework/AudioManage";
import sceneMediator from "../framework/sceneMediator";

const {ccclass, property} = cc._decorator;

export enum btnType {
    normal = 0,
    press = 1,
    pressBack = 2,
    pressTouch = 3,
}

@ccclass
export default class pressBtnMediator extends cc.Component {

    @property({type : cc.Enum(btnType),displayName : "当前按钮的类型"})
    curBtnType : btnType = btnType.normal;

    @property(cc.Node)
    moveNode : cc.Node = null;

    @property({type : cc.Component.EventHandler})
    toolEvent : cc.Component.EventHandler[] = [];

    isCheck : boolean = false;

    @property()
    isClose : boolean = false;

    closePress : boolean = false;

    isTouch = false;
    isPress = false;

    protected onLoad(): void {
        if(this.curBtnType == btnType.press){
            this.node.parent.getChildByName("touchNode").on("touchstart",(event)=>{
                if(this.closePress) return;
                cc.tween(this.node).by(0.3,{
                    position : cc.v2(0,-10)
                }).start();
                if(this.isClose){
                    this.closePress = true;
                }
                cc.Component.EventHandler.emitEvents(this.toolEvent, [this.node]);
            },this);
        }

        if(this.curBtnType == btnType.pressTouch){
            this.node.parent.getChildByName("touchNode").on("touchstart",(event)=>{
                if(!this.isPress && !this.isTouch){
                    this.isTouch = true;
                    this.moveNode.y = -10;
                    cc.Component.EventHandler.emitEvents(this.toolEvent, event);
                }
            },this);

            this.node.parent.getChildByName("touchNode").on("touchend",(event)=>{
                if(!this.isPress && this.isTouch){
                    this.isTouch = false;
                    this.moveNode.y = 5;
                    cc.Component.EventHandler.emitEvents(this.toolEvent, event);
                }
            },this);

            this.node.parent.getChildByName("touchNode").on("touchcancel",(event)=>{
                if(!this.isPress && this.isTouch){
                    this.isTouch = false;
                    this.moveNode.y = 5;
                    cc.Component.EventHandler.emitEvents(this.toolEvent, event);
            }
            },this);
        }
    }

    //
    isMoving = false;
    isStabTouch = false;
    isRoleBox = false;
    onBeginContact(contact, self, other) {
        if(this.isCheck) return;
        if(other.node.group == "roleBox"){
            // this.isCheck = true;
            this.isRoleBox = true;
            this.moveNode.y = -10;
            cc.Component.EventHandler.emitEvents(this.toolEvent,[1]);
            return;
        }
        if(this.isRoleBox) return;
        if(other.node.group == "role" || other.node.group == "key" ){
            switch (this.curBtnType) {
                case btnType.normal :
                    AudioManage.playSFX(soundList.buttonPress,false);
                    cc.tween(this.node).by(0.3,{
                        position : cc.v2(0,-10)
                    }).start();
                    cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
                    this.isCheck = true;
                    break;
                case btnType.press :
                    AudioManage.playSFX(soundList.buttonPress,false);
                    this.isCheck = true;
                    break;
                case btnType.pressBack :
                    if(sceneMediator.getShowScene() == 48 || sceneMediator.getShowScene() == 47){
                        if(this.isStabTouch) return;
                        this.isCheck = true;
                    }
                    this.moveNode.y = -10;
                    cc.Component.EventHandler.emitEvents(this.toolEvent,[1]);
                    break;
                case btnType.pressTouch :
                    if(!this.isPress && !this.isTouch){
                        this.isPress = true;
                        this.moveNode.y = -10;
                        cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
                    }
                    break;
            }
        }

        if(this.curBtnType == btnType.pressBack && other.node.group == "default"){
            if(this.isCheck) return;
            let scriptComponent = other.node.getComponent("defaultTypeMediator");
            if(scriptComponent && scriptComponent.curType == defaultType.stab){
                this.moveNode.y = -10;
                this.isStabTouch = true;
                cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
            }
        }


        if(this.curBtnType == btnType.pressBack && other.node.group == "default"){
            let scriptComponent = other.node.getComponent("defaultTypeMediator");
            if(scriptComponent && scriptComponent.curType == defaultType.box){
                this.moveNode.y = -10;
                cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
            }
        }

        if(this.curBtnType == btnType.pressTouch && other.node.group == "default"){
            let scriptComponent = other.node.getComponent("defaultTypeMediator");
            if(scriptComponent && scriptComponent.curType == defaultType.box){
                if(!this.isPress && !this.isTouch){
                    this.isPress = true;
                    this.moveNode.y = -10;
                    cc.Component.EventHandler.emitEvents(this.toolEvent, event);
                }
            }
        }
    }

    onEndContact(contact, self, other) {
        if(other.node.group == "roleBox"){
            this.isRoleBox = false;
            this.moveNode.y = 5;
            cc.Component.EventHandler.emitEvents(this.toolEvent,[2]);
            return;
        }
        if(this.isRoleBox) return;
        if(other.node.group == "role" || other.node.group == "key" ){
            if(this.curBtnType == btnType.pressBack){
                if(sceneMediator.getShowScene() == 48 || sceneMediator.getShowScene() == 47 ){
                    if(!this.isCheck)  return;
                    this.isCheck = false;
                }
                this.moveNode.y = 5;
                cc.Component.EventHandler.emitEvents(this.toolEvent,[2]);
            }

            if(this.curBtnType == btnType.pressTouch){
                if(this.isPress && !this.isTouch){
                    this.isPress = false;
                    this.moveNode.y = 5;
                    cc.Component.EventHandler.emitEvents(this.toolEvent, event);
                }
            }
        }

        if(this.curBtnType == btnType.pressBack && other.node.group == "default"){
            if(this.isCheck) return;
            let scriptComponent = other.node.getComponent("defaultTypeMediator");
            if(scriptComponent && scriptComponent.curType == defaultType.stab){
                this.moveNode.y = 5;
                this.isStabTouch = false;
                cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
            }
        }

        if(this.curBtnType == btnType.pressBack && other.node.group == "default"){
            let scriptComponent = other.node.getComponent("defaultTypeMediator");
            if(scriptComponent && scriptComponent.curType == defaultType.box){
                this.moveNode.y = 5;
                cc.Component.EventHandler.emitEvents(this.toolEvent,[]);
            }
        }

        if(this.curBtnType == btnType.pressTouch && other.node.group == "default"){
            let scriptComponent = other.node.getComponent("defaultTypeMediator");
            if(scriptComponent && scriptComponent.curType == defaultType.box){
                if(this.isPress && !this.isTouch){
                    this.isPress = false;
                    this.moveNode.y = 5;
                    cc.Component.EventHandler.emitEvents(this.toolEvent, event);
                }
            }
        }
    }

}
